package project1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Gomoku_Component("登录界面")
public class Login {
    public static void main(String[]args){
        JFrame frame = new JFrame("登录");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(400,300,600,400);
        frame.setBackground(Color.WHITE);

        @Interface_component(name = "主窗口，使用BorderLayout布局")
        JPanel LoginPanel = new JPanel(new BorderLayout());
        LoginPanel.setBackground(Color.gray);
        LoginPanel.setBorder(BorderFactory.createEmptyBorder(100,80,100,100));


        //表单面板
        //vgap改变表格的垂直间距
        //hgap改变表格的水平间距
        @Interface_component(name = "表单面板设置登录注册的，使用GridLayout布局")
        JPanel  formPanel = new JPanel(new GridLayout(2,2,0,10));
        formPanel.setBackground(Color.red);
        formPanel.setBorder(BorderFactory.createEmptyBorder(60,10,10,130));
        LoginPanel.add(formPanel);

        @Interface_component(name = "登录按钮面板，使用FlowLayout布局")
        //登录面板
        JPanel ButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,0,0));
        ButtonPanel.setBackground(Color.green);
        ButtonPanel.setBorder(BorderFactory.createEmptyBorder(0,55,10,50));
        LoginPanel.add(ButtonPanel,BorderLayout.SOUTH);

        @Interface_component(name = "用户名标签")
        JLabel Label_username = new JLabel("用户名");
        Label_username.setBorder(BorderFactory.createEmptyBorder(20,80,20,0));
        JTextField username = new JTextField(15);

        @Interface_component(name = "密码标签")
        JLabel Label_password = new JLabel("密码");
        Label_password.setBorder(BorderFactory.createEmptyBorder(5,80,5,0));
        JPasswordField password = new JPasswordField(15);


        @Interface_component(name = "登录按钮")
        JButton btn_login = new JButton("登录");
        btn_login.setBackground(new Color(70, 130, 180));
        btn_login.setForeground(Color.WHITE);
        btn_login.setFont(new Font("宋体", Font.BOLD, 14));
        btn_login.setFocusPainted(false);
        btn_login.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(39, 174, 96), 6),
                BorderFactory.createEmptyBorder(8, 23, 8, 23)));


        btn_login.addActionListener(new ActionListener() {
            @Interface_event(event = "登录按钮点击事件")
            @Override
            public void actionPerformed(ActionEvent e) {
                String Username = username.getText();
                String Password = String.valueOf(password.getPassword());
                System.out.println(Username);
                System.out.println(Password);
                System.out.println("登录");
                if(Username.isEmpty() || Password.isEmpty()){
                    JOptionPane.showMessageDialog(frame, "请输入用户名和密码");
                    return;
                }
                try{
                    seek(Username ,Password);
                }catch(SQLException se){
                    se.printStackTrace();
                }


            }

            @Interface_event(event = "判断是否注册，查询数据库")
            public void seek(String username, String password) throws SQLException {
                Mysql conn = new Mysql();
                conn.getConnection();
                PreparedStatement ps = conn.con.prepareStatement("select * from admin where username=? and password=?");
                ps.setString(1, username);
                ps.setString(2, password);
                ResultSet rs = ps.executeQuery();
                if(rs.next()) {
                    System.out.println("登录成功！");
                    Gomoku g = new Gomoku();
                    frame.dispose();
                    g.main(new String[]{"acb", "acv"});

                }
                else{
                    System.out.println("用户名或密码错误！");
                }
                if(ps!=null)
                    ps.close();
                if(conn!=null)
                    conn.con.close();
            }

        });


        ButtonPanel.add(btn_login);
        formPanel.add(Label_username);
        formPanel.add(username);
        formPanel.add(Label_password);
        formPanel.add(password);
        frame.add(LoginPanel);

        frame.setVisible(true);
    }


}
